﻿using System;
using System.Collections.Generic;
using System.Data;
using ContractManager.Common.Class;
using ContractManager.Entity;
using Speed.Data;

namespace ContractManager.BLL.Business
{
    public static class ClsDocuments
    {
        public static List<Documents> SelectAll()
        {
            using (var db = Sys.NewDb())
            {
                return Data.Base.Documents.Select(db);
            }
            
        }

        public static DataTable GetAll(int formNo, int voucherCate, DateTime fromDate, DateTime toDate)
        {
            using (var db = Sys.NewDb())
            {
                string commandText = String.Format("EXEC dbo.usp_Documents_GetAll {0},{1},{2},{3},'{4}','{5}'",
                                            ClsVariable.AsSysMyCoYearId,
                                            formNo,
                                            voucherCate,
                                            ClsVariable.EmployeeId,
                                            fromDate.ToString("dd/MMM/yyyy"),
                                            toDate.ToString("dd/MMM/yyyy"));
                return db.ExecuteDataTable(commandText);
            }

        }

        public static DataTable ListDetail(Int32 revId)
        {
            using (var db = Sys.NewDb())
            {
                string commandText = String.Format("EXEC dbo.usp_DocumentDetails_SelectByMaster {0}", revId);
                return db.ExecuteDataTable(commandText);
            }

        }

        public static void Insert(Documents capital)
        {
            using (var db = Sys.NewDb())
            {
                db.BeginTransaction();
                db.Insert<Documents>(capital, EnumSaveMode.Requery);

                foreach (var addItem in capital.ListAdd)
                {
                    addItem.DocId = capital.DocId;
                    db.Insert<DocumentDetails>(addItem);
                }
                foreach (var updateItem in capital.ListUpdate)
                {
                    updateItem.DocId = capital.DocId;
                    db.Update<DocumentDetails>(updateItem);
                }
                foreach (var delItem in capital.ListDelete)
                {
                    db.Delete<DocumentDetails>(string.Format("DocDetId={0}", delItem.DocDetId));
                }

                db.Commit();
            }

            //using (var db = Sys.NewDb())
            //{
            //    db.Insert<Documents>(capital, EnumSaveMode.Requery);
            //}
            
        }

        public static void Update(Documents capital)
        {
            using (var db = Sys.NewDb())
            {
                db.BeginTransaction();
                db.Update<Documents>(capital);
                foreach (var addItem in capital.ListAdd)
                {
                    addItem.DocId = capital.DocId;
                    db.Insert<DocumentDetails>(addItem);
                }
                foreach (var updateItem in capital.ListUpdate)
                {
                    updateItem.DocId = capital.DocId;
                    db.Update<DocumentDetails>(updateItem);
                }
                foreach (var delItem in capital.ListDelete)
                {
                    db.Delete<DocumentDetails>(string.Format("DocDetId={0}", delItem.DocDetId));
                }
                db.Commit();
            }
            //using (var db = Sys.NewDb())
            //{
            //    db.Update<Documents>(capital);
            //}
            
        }

        public static Documents SelectByPk(Int32 docId)
        {
            using (var db = Sys.NewDb())
            {
                return db.SelectSingle<Documents>(string.Format("DocId={0}", docId));
            }
            
        }

        public static int DeleteByPk(Int32 docId)
        {
            using (var db = Sys.NewDb())
            {
                db.BeginTransaction();
                int ret = db.Delete<Documents>(string.Format("DocId={0}", docId));
                int rowCount = db.Delete<DocumentDetails>(string.Format("DocId={0}", docId));
                db.Commit();
                return ret;
            }

            //using (var db = Sys.NewDb())
            //{
            //    return db.Delete<Documents>(string.Format("DocId={0}", docId));
            //}
            
        }
    }
}